﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Brain.Rendering
{
    public class Shape_BoundingFrustum : Shape
    {
        public VertexPositionColor[] Vertices { get; protected set; }

        public Vector3[] Corners { get; protected set; }

        public bool UseVertexColor { get; set; }

        public Shape_BoundingFrustum()
        {
            Vertices = new VertexPositionColor[24];
            Corners = new Vector3[8];

            UseVertexColor = true;
        }

        public void Draw(object obj, Color color, ICamera3D Camera, ShapeDrawer drawer, Effect effect)
        {
            BoundingFrustum frustum = (BoundingFrustum)obj;

            frustum.GetCorners(Corners);

            Vertices[0] = new VertexPositionColor(Corners[0], color);
            Vertices[1] = new VertexPositionColor(Corners[1], color);
            Vertices[2] = new VertexPositionColor(Corners[1], color);
            Vertices[3] = new VertexPositionColor(Corners[2], color);
            Vertices[4] = new VertexPositionColor(Corners[2], color);
            Vertices[5] = new VertexPositionColor(Corners[3], color);
            Vertices[6] = new VertexPositionColor(Corners[3], color);
            Vertices[7] = new VertexPositionColor(Corners[0], color);

            Vertices[8] = new VertexPositionColor(Corners[4], color);
            Vertices[9] = new VertexPositionColor(Corners[5], color);
            Vertices[10] = new VertexPositionColor(Corners[5], color);
            Vertices[11] = new VertexPositionColor(Corners[6], color);
            Vertices[12] = new VertexPositionColor(Corners[6], color);
            Vertices[13] = new VertexPositionColor(Corners[7], color);
            Vertices[14] = new VertexPositionColor(Corners[7], color);
            Vertices[15] = new VertexPositionColor(Corners[4], color);

            Vertices[16] = new VertexPositionColor(Corners[0], color);
            Vertices[17] = new VertexPositionColor(Corners[4], color);
            Vertices[18] = new VertexPositionColor(Corners[1], color);
            Vertices[19] = new VertexPositionColor(Corners[5], color);
            Vertices[20] = new VertexPositionColor(Corners[2], color);
            Vertices[21] = new VertexPositionColor(Corners[6], color);
            Vertices[22] = new VertexPositionColor(Corners[3], color);
            Vertices[23] = new VertexPositionColor(Corners[7], color);

            for (int i = 0; i < effect.CurrentTechnique.Passes.Count; i++)
            {
                EffectPass pass = effect.CurrentTechnique.Passes[i];

                pass.Apply();
                GraphicsDevice device = Engine.Instance.GraphicsDevice;
                device.DrawUserPrimitives(PrimitiveType.LineList, Vertices, 0, Vertices.Length / 2);
            }
        }
    }
}
